class OperationController < ApplicationController

  def newoperation
    operation = params['operationId']

    if operation == '1'
      #Purchase
      @group = Group.find(params['groupId'])      
      session[:group] = @group

      if not params['subgroupId'].nil?
        @subgroup = Subgroup.find(params['subgroupId'])      
        session[:subgroup] = @subgroup
        render :action => 'purchaseRights'
      end
      render :action => 'purchase'

    elsif operation == '2'
      #Sale
      @group = Group.find(params['groupId'])
      session[:group] = @group
      render :action => 'sale'
    elsif operation == '3'
      #Split
      @group = Group.find(params['groupId'])
      session[:group] = @group
      render :action => 'split'
    elsif operation == '4'
      #Right To Acquire
      @group = Group.find(params['groupId'])
      session[:group] = @group
      render :action => 'rightToAcquire'
    else
      redirect_to :controller => 'index', :action => 'index'
    end
  end


  def purchaseAction
    @operation = Operation.new(params[:operation])
    @group = session[:group]
    
    @operation.operation_type_id = 1
    @operation.group_id = @group.id
    @operation.share_id = @group.share_id

    if @operation.valid? && @group.purchase(@operation)      
      redirect_to :controller => 'index', :action => 'index'
    else
      render :action => 'purchase'    
    end
  end

  def purchaseNewAction
    success = false
    @operation = Operation.new(params[:operation])
    group = Group.new(params[:group])
    if not group.share_id.nil?
      success = true
      shareId = group.share_id
      accountId = group.account_id
    end

    if success && @operation.valid?
      group = Group.byOpenShare(shareId)

      if group.nil?        
        #We need to create new group
        group = Group.init
        group.share_id = shareId
        group.account_id = accountId
      end

      @operation.operation_type_id = 1
      if group.purchase(@operation)      
        success = true
      end
    else
      success = false
    end

    if success
      redirect_to :controller => 'index', :action => 'index'
    else
      render :action => 'purchaseNew'    
    end

  end


  def saleAction
    @operation = Operation.new(params[:operation])
    success = true
    @group = session[:group]
    if @operation.quantity > @group.quantity
      @operation.errors.add(:quantity, "You can't sell more than you have!.")
      success = false
    end

    @operation.operation_type_id = 2
    @operation.group_id = @group.id
    @operation.share_id = @group.share_id

    if success && @operation.valid? && @group.sale(@operation)      
      redirect_to :controller => 'index', :action => 'index'
    else
      render :action => 'sale'    
    end
  end

  def splitAction
    @split = Split.new(params[:split])
    @operation = Operation.new(params[:operation])
    @operation.price = 0
    @operation.brokerage = 0
    @operation.quantity = 0
    @group = session[:group]
    success = false

    if @split.valid?
      success = true
    end
    
    if success && @group.split(@split, @operation)
      redirect_to :controller => 'index', :action => 'index'
    else
      render :action => 'split'    
    end

  end

  def rightToAcquireAction
    @operation = Operation.new(params[:operation])
    @operation.price = 0
    @operation.brokerage = 0
    @operation.operation_type_id = 4

    @group = session[:group]
    success = false

    if @operation.valid?
      success = true
    end
    if @operation.share_id.nil?
      @operation.errors.add(:share_id, "Right Name can't be blank")
      success = false
    end

    if success && @group.rightToAcquire(@operation)
      redirect_to :controller => 'index', :action => 'index'
    else
      render :action => 'rightToAcquire'
    end

    
  end

end
